package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import entities.Application;
import entities.ApplicationConfig;

import admin.forms.ApplicationConfigForm;
import admin.models.ApplicationConfigModel;
import admin.models.ApplicationModel;

public class ApplicationConfigEditServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		TreeMap check = new TreeMap();
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/admin/appconfig/list");
			return ;
		}
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(id);
		
		if (app == null) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		//Get template
		
		//Get AppConfig
		ApplicationConfigModel appConfigModel = ApplicationConfigModel.getInstance();
		ApplicationConfig appConfig = appConfigModel.get(id, id);
		
		if (appConfig == null) {
			appConfig = new ApplicationConfig();
			appConfig.setApplicationId(id);
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("appConfig", appConfig);
		try {
			request.getRequestDispatcher("/admin/pages/application_config_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(id);
		
		if (app == null) {
			response.sendRedirect("/admin/application/list");
			return ;
		}
		
		
		//Get AppConfig
		ApplicationConfigModel appConfigModel = ApplicationConfigModel.getInstance();
		ApplicationConfig appConfig = appConfigModel.get(id, id);
		
		if (appConfig == null) {
			appConfig = new ApplicationConfig();
			appConfig.setApplicationId(id);
		}
		
		TreeMap form = (TreeMap) ApplicationConfigForm.checkValid(request, appConfig);
		appConfig = (ApplicationConfig) form.get("appConfig");
		TreeMap check = (TreeMap) form.get("checkValid");
		
		if (check.size() == 0) {
			appConfigModel.insert(id, appConfig);
			request.setAttribute("success", "Update success application config " + appConfig.getApplicationId());
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("appConfig", appConfig);
		try {
			request.getRequestDispatcher("/admin/pages/application_config_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
